package shipin.main;


import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacpp.helper.opencv_core.AbstractIplImage;

import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.Buffer;

import javax.imageio.ImageIO;
import javax.swing.Timer;

import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameConverter;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber;  

public class Dome3 {
	
	public static String ipname = "192.168.1.106";  
	
	public static boolean isStart = false;
    
    //timer for image capture animation  
    static class TimerAction implements ActionListener {  
        private Graphics2D g;  
        private CanvasFrame canvasFrame;  
        private int width,height;  
          
        private int delta=10;  
        private int count = 0;  
          
        private Timer timer;  
        public void setTimer(Timer timer){  
            this.timer = timer;  
        }
        public TimerAction(CanvasFrame canvasFrame){  
            this.g = (Graphics2D)canvasFrame.getCanvas().getGraphics();   
            this.canvasFrame = canvasFrame;  
            this.width = canvasFrame.getCanvas().getWidth();  
            this.height = canvasFrame.getCanvas().getHeight();  
        }  
        public void actionPerformed(ActionEvent e) {  
            int offset = delta*count;  
            if(width-offset>=offset && height-offset >= offset) {          
                g.drawRect(offset, offset, width-2*offset, height-2*offset);  
                canvasFrame.repaint();  
                count++;  
            }else{  
                //when animation is done, reset count and stop timer.  
                timer.stop();  
                count = 0;  
            }              
        }    
    }
    private static FrameConverter converter = new OpenCVFrameConverter.ToIplImage();
    private static Java2DFrameConverter javaConverter = new Java2DFrameConverter();
	public static void main(String[] args) throws Exception {
		ipname = Utils.getInstance().getString("hostname");
		System.out.print(ipname);
		test();
	} 
	private static MyThread th = null;
	public static void test() throws Exception{
		ServerSocket ss = new ServerSocket(6666);
		OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);  
        grabber.start();  
          
        //create a frame for real-time image display  
        CanvasFrame canvasFrame = new CanvasFrame("Camera"); 
        Frame frame = grabber.grab();
        IplImage image = (IplImage)converter.convert(frame); 
        int width = image.width();  
        int height = image.height();  
        canvasFrame.setCanvasSize(width, height);  
          
        //onscreen buffer for image capture  
        final BufferedImage bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        Graphics2D bGraphics = bImage.createGraphics();       
          
        //animation timer  
        TimerAction timerAction = new TimerAction(canvasFrame);  
        final Timer timer=new Timer(10, timerAction);  
        timerAction.setTimer(timer);  

        //real-time image display  
        while(canvasFrame.isVisible() && (frame=grabber.grab()) != null){  
            if(!timer.isRunning()) { //when animation is on, pause real-time display  
                canvasFrame.showImage(frame);     
                //draw the onscreen image simutaneously 
                BufferedImage bufferedImage = javaConverter.convert(frame);
                bGraphics.drawImage(bufferedImage,null,0,0); 
//                Buffer[] buffereds = frame.image;
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage,"jpg",out);
                out.flush();
                if(out!=null){
					if (th == null) {
						th = new MyThread(out, ipname, ss, null);
						th.start();
					}else{
						if(th.isConnected){
							th = new MyThread(out, ipname, ss, null);
							th.start();
						}
					}
				}
            }  
        }  
          
        //release resources  
//        cvReleaseImage(image); 
        
        grabber.stop();  
        canvasFrame.dispose();  
	}		
}  
